package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        ServiceItem serviceItem1 = new ServiceItem();
        serviceItem1.setName(serviceItem.getName());
        serviceItem1.setCarPackage(serviceItem.getCarPackage());
        serviceItem1.setDiscountPrice(serviceItem.getDiscountPrice());
        serviceItem1.setOriginalPrice(serviceItem.getOriginalPrice());
        serviceItem1.setServiceCatalog(serviceItem.getServiceCatalog());
        serviceItem1.setInfo(serviceItem.getInfo());
        serviceItem1.setCreateTime(new Date());

        //根据套餐来判断是否是套餐
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) {
            serviceItem1.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else {
            serviceItem1.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }

        serviceItemMapper.insert(serviceItem1);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem serviceItem1 = this.get(serviceItem.getId());

        if (ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem1.getAuditStatus()) || ServiceItem.SALESTATUS_ON.equals(serviceItem1.getSaleStatus())){
            throw new BusinessException("非法操作");
        }

        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem1.getCarPackage()) && ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem1.getAuditStatus())) {
            serviceItem1.setName(serviceItem.getName());
            serviceItem1.setOriginalPrice(serviceItem.getOriginalPrice());
            serviceItem1.setDiscountPrice(serviceItem.getDiscountPrice());
            serviceItem1.setInfo(serviceItem.getInfo());
            serviceItem1.setServiceCatalog(serviceItem.getServiceCatalog());
            serviceItem1.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
            serviceItemMapper.updateByPrimaryKey(serviceItem1);
        }else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
            serviceItemMapper.updateByPrimaryKey(serviceItem);
        }

    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }


    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = this.get(id);

        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) && !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("非法操作");
        }

        serviceItemMapper.saleOnUpdate(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void saleOff(Long id) {
        serviceItemMapper.saleOnUpdate(id,ServiceItem.AUDITSTATUS_INIT);
    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, String info,Long finance) {
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);

        if (ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())) {
            throw new BusinessException("非法操作");
        }

        if (ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus()) || ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())) {
            throw  new BusinessException("非法操作");
        }

        //把信息保存到car表中
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setServiceItemId(serviceItem.getId());
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setInfo(info);
        carPackageAudit.setAuditorId(director);
        carPackageAudit.setBpmnInfoId(bpmnInfoId);
        carPackageAudit.setServiceItemPrice(serviceItem.getOriginalPrice());
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        carPackageAuditMapper.insert(carPackageAudit);
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(bpmnInfoId);
        Map<String, Object> map = new HashMap<>();
        map.put("director",director);
        if (finance != null) {
            map.put("finance",finance);
        }
        map.put("discountPrice", serviceItem.getDiscountPrice().longValue());
        //启动实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId() + "", map);
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditMapper.updateByInstanceId(carPackageAudit.getId(),carPackageAudit.getInstanceId());
        //修改状态为审核中
        serviceItemMapper.updateByStutas(serviceItem.getId(),ServiceItem.AUDITSTATUS_AUDITING);
    }
}
